package Interpretor;

import arbori.Nod;
import functii.*;
import functii.elementare.*;
import functii.trigonometrice.*;
import operatori.*;

public class CalculDerivataContext extends Context {
	private double result;
	private CalculFunctionContext cfc;

	public double getResult() {
		return result;
	}

	public void setResult(double result1) {
		result = result1;
	}


	public void actual(Nod nod) {
		if (nod instanceof Plus)
			actual((Plus) nod);
		if (nod instanceof Minus)
			actual((Minus) nod);
		if (nod instanceof Ori)
			actual((Ori) nod);
		if (nod instanceof Impartit)
			actual((Impartit) nod);
		if (nod instanceof Sin)
			actual((Sin) nod);
		if (nod instanceof Cos)
			actual((Cos) nod);
		if (nod instanceof Logaritm)
			actual((Logaritm) nod);
		if (nod instanceof Necunoscuta)
			actual((Necunoscuta) nod);
		if (nod instanceof Constanta)
			actual((Constanta) nod);
		if (nod instanceof Putere)
			actual((Putere) nod);
		if (nod instanceof Radical)
			actual((Radical) nod);
	}


	public void actual(Plus plus) {
		if (plus.getFStang() != null && plus.getFDrept() != null) {
			plus.getFStang().interpret(this);
			double s1 = result;
			plus.getFDrept().interpret(this);
			double s2 = result;

			result = s1 + s2;
		}

	}


	public void actual(Minus minus) {
		if (minus.getFStang() != null && minus.getFDrept() != null) {
			minus.getFStang().interpret(this);
			double s1 = result;
			minus.getFDrept().interpret(this);
			double s2 = result;

			result = s1 + s2;
		}

	}


	public void actual(Ori multiply) {
		if (multiply.getFStang() != null && multiply.getFDrept() != null) {
			multiply.getFStang().interpret(this);
			double s1 = result;
			multiply.getFDrept().interpret(this);
			double s2 = result;

			cfc = new CalculFunctionContext();
			multiply.getFStang().interpret(cfc);
			double res1 = cfc.getResult();
			multiply.getFDrept().interpret(cfc);
			double res2 = cfc.getResult();
			result = s1 * res2 + res1 * s2;
		}
	}

	public void actual(Impartit simplify) {
		if (simplify.getFStang() != null && simplify.getFDrept() != null) {
			simplify.getFStang().interpret(this);
			double s1 = result;
			simplify.getFDrept().interpret(this);
			double s2 = result;

			cfc = new CalculFunctionContext();
			simplify.getFStang().interpret(cfc);
			double res1 = cfc.getResult();
			simplify.getFDrept().interpret(cfc);
			double res2 = cfc.getResult();
			if (res2 != 0) {
				result = (s1 * res2 - res1 * s2) / res2 * res2;
			} else {
				System.exit(1);
			}
		}

	}


	public void actual(Constanta constanta) {
		result = 0;
	}


	public void actual(Logaritm logaritm) {
		// TODO Auto-generated method stub

	}


	public void actual(Putere putere) {
		// TODO Auto-generated method stub

	}


	public void actual(Radical radical) {
		// TODO Auto-generated method stub

	}


	public void actual(Necunoscuta necunoscuta) {
		result = 1;
	}


	public void actual(Sin sin) {
		if (sin.getFStang() != null) {
			sin.getFStang().interpret(this);
			double s1 = result;
			cfc = new CalculFunctionContext();
			sin.getFStang().interpret(cfc);
			double res1 = cfc.getResult();

			result = s1 * Math.cos(res1);
		}
	}

	public void actual(Cos cos) {
		if (cos.getFStang() != null) {
			cos.getFStang().interpret(this);
			double s1 = result;
			cfc = new CalculFunctionContext();
			cos.getFStang().interpret(cfc);
			double res1 = cfc.getResult();

			result = s1 * (-Math.sin(res1));
		}
	}
}
